#!/bin/bash

red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
color_end="\033[0m"

if [[ $EUID -ne 0 ]]; then
    echo "Error: This script must be run as root!" 1>&2
    exit 1
fi

ROOT_PATH=`pwd`
PROJECT_ROOT="${ROOT_PATH}/index"
URL_GITEE="https://gitee.com/vincent-repo/index.git"
URL_GITHUB="http://github.com/SeetaFace6Open/index.git"

if ! type yum >/dev/null 2>&1; then
    PACKAGING="apt"
else
    PACKAGING="yum"
fi

function donothing(){
    return 0;
}

function help_message(){
    echo -e "${green}Usage:${color_end} ${yellow}${0} <platform> [repository]${color_end}"
    echo -e "    ${green}platform:${color_end}   ${yellow}linux or android${color_end}"
    echo -e "    ${green}repository:${color_end} ${yellow}gitee or github default is github${color_end}"
    echo -e "${green}eg:${color_end} "
    echo -e "    ${yellow}${0} linux${color_end}"
    echo -e "    ${yellow}${0} linux github${color_end}"
    echo -e "    ${yellow}${0} linux gitee${color_end}"
    echo -e "    ${yellow}${0} android${color_end}"
    echo -e "    ${yellow}${0} adnroid github${color_end}"
    echo -e "    ${yellow}${0} android gitee${color_end}"
}

function init_param_android(){
    NDK_PATH="/usr/ndk/android-ndk-r22b"
    NDK_BUILD="${NDK_PATH}/ndk-build"
    NDK_BUILD_ARGS="-j`cat /proc/cpuinfo | grep 'cpu cores' | wc -l`"
    OUTPUT_PATH="${ROOT_PATH}/libs/android"
}

function init_param_linux(){
    SHELL_FILE="build.linux.x64.sh"
    BUILD="bash ${SHELL_FILE}"
    CHMOD="find ${PROJECT_ROOT} -name ${SHELL_FILE} -exec chmod +x {} ;"
    OUTPUT_PATH="${ROOT_PATH}/libs/linux"
}

function clean_output_path(){
    rm -rf ${OUTPUT_PATH}
    mkdir -p ${OUTPUT_PATH}
}

function install_development_tools(){
    if ! type gcc >/dev/null 2>&1; then
        if [[ ${PACKAGING} == "yum" ]]; then
            ${PACKAGING} groupinstall -y "Development Tools";
        else
            ${PACKAGING} install -y build-essential libtool automake autoconf;
        fi
    fi
    if ! type git >/dev/null 2>&1; then
        ${PACKAGING} install -y git
    fi
    if ! type cmake >/dev/null 2>&1; then
        ${PACKAGING} install -y cmake
    fi
    if ! type sed >/dev/null 2>&1; then
        ${PACKAGING} install -y sed
    fi
}

function download_project(){
    if [[ ! -d ${PROJECT_ROOT} ]]; then
        if [[ ${1} == "gitee" ]]; then
            git clone ${URL_GITEE};
        elif [[ ${1} == "github" ]]; then
            git clone --recursive ${URL_GITHUB};
        else
            echo -e "${green}not select any download url, default download from github${color_end}";
            git clone --recursive ${URL_GITHUB};
        fi
    fi
}

function fix_project_bug(){
    if [[ `cat ${PROJECT_ROOT}/OpenRoleZoo/include/orz/mem/pot.h | head -n 10 | tail -n 1` == "" ]]; 
        then sed -i '9a #include <functional>' ${PROJECT_ROOT}/OpenRoleZoo/include/orz/mem/pot.h; 
    fi
}

function build_project_linux_base(){
    cd ${PROJECT_ROOT}/${1}/craft
    ${BUILD}
}

function build_project_android_base(){
    cd ${PROJECT_ROOT}/${1}/android/jni
    ${NDK_BUILD} ${NDK_BUILD_ARGS}
    cp -r ${PROJECT_ROOT}/${1}/android/libs/* ${OUTPUT_PATH}/
}

function build_project_linux_common(){
    build_project_linux_base OpenRoleZoo
    build_project_linux_base SeetaAuthorize
    build_project_linux_base TenniS
}

function build_project_android_common(){
    build_project_android_base OpenRoleZoo
    build_project_android_base SeetaAuthorize
    build_project_android_base TenniS
}

function build_project_linux(){
    build_project_linux_base FaceBoxes
    build_project_linux_base Landmarker
    build_project_linux_base FaceRecognizer6
    build_project_linux_base PoseEstimator6
    build_project_linux_base FaceTracker6
    build_project_linux_base FaceAntiSpoofingX6
    build_project_linux_base QualityAssessor3
    build_project_linux_base SeetaAgePredictor
    build_project_linux_base SeetaEyeStateDetector
    build_project_linux_base SeetaGenderPredictor
    build_project_linux_base SeetaMaskDetector
}

function build_project_android(){
    build_project_android_base FaceBoxes/FaceDetector
    build_project_android_base Landmarker/Landmarker
    build_project_android_base FaceRecognizer6/FaceRecognizer
    build_project_android_base PoseEstimator6/PoseEstimation
    build_project_android_base FaceTracker6/FaceTracking
    build_project_android_base FaceAntiSpoofingX6/FaceAntiSpoofingX
    build_project_android_base QualityAssessor3/QualityAssessor
    build_project_android_base SeetaAgePredictor/AgePredictor
    build_project_android_base SeetaEyeStateDetector/EyeStateDetector
    build_project_android_base SeetaGenderPredictor/GenderPredictor
    build_project_android_base SeetaMaskDetector/MaskDetector
}

function clean_project_linux(){
    find ${PROJECT_ROOT} -name build.linux.x64 -exec rm -rf {} \; 2>&1 1>/dev/null
}

function clean_project_android(){
    rm -rf ${PROJECT_ROOT}/OpenRoleZoo/android/libs >/dev/null 2>&1
    rm -rf ${PROJECT_ROOT}/OpenRoleZoo/android/obj >/dev/null 2>&1
    rm -rf ${PROJECT_ROOT}/SeetaAuthorize/android/libs >/dev/null 2>&1
    rm -rf ${PROJECT_ROOT}/SeetaAuthorize/android/obj >/dev/null 2>&1
    rm -rf ${PROJECT_ROOT}/TenniS/android/libs >/dev/null 2>&1
    rm -rf ${PROJECT_ROOT}/TenniS/android/obj >/dev/null 2>&1
}

if [[ ${1} == "linux" ]]; then
    init_param_linux
elif [[ ${1} == "android" ]]; then
    init_param_android
else
    help_message
    exit 0
fi
clean_output_path
install_development_tools
download_project ${2}
fix_project_bug
if [[ ${1} == "linux" ]];
then
    ${CHMOD} ;
    build_project_linux_common ;
    build_project_linux ;
    build_project_linux_base SeetaMaskDetector ;
    cp -r ${PROJECT_ROOT}/build/* ${OUTPUT_PATH}/ ;
    cp -r ${PROJECT_ROOT}/SeetaMaskDetector/build/* ${OUTPUT_PATH}/ ;
    clean_project_linux ;
elif [[ ${1} == "android" ]]; then
    build_project_android_common ;
    build_project_android ;
    cp -r ${PROJECT_ROOT}/OpenRoleZoo/android/obj/local/arm64-v8a/libORZ_static.a ${OUTPUT_PATH}/arm64-v8a/
    cp -r ${PROJECT_ROOT}/OpenRoleZoo/android/obj/local/armeabi-v7a/libORZ_static.a ${OUTPUT_PATH}/armeabi-v7a/
    clean_project_android ;
else
    help_message
    exit 0
fi
clear
echo -e "${yellow}Build finished and there are output folder/file(s)!${color_end}"
ls ${OUTPUT_PATH}
